
import * as THREE from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader";
import { OrbitControls } from "three/addons/controls/OrbitControls";

const initMethod = {
  initThree:() => {
    console.log('初始化three======》');
    // 1.创建场景
    const scene = new THREE.Scene();
    // scene.background = 0x0000ff;
    scene.background = new THREE.Color("white");
    // 2.创建相机
    const camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 200 );
    // 2.1设置相机的初始位置
    camera.position.z = 5;
    // 3.设置渲染空间大小
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize( window.innerWidth, window.innerHeight );
    document.body.appendChild( renderer.domElement );
    // 加载荔枝模型
    const loader = new GLTFLoader();
    loader.load( '/nuomici/scene.gltf', function ( gltf ) {
      scene.add( gltf.scene );
    }, undefined, function ( error ) {
      console.error( error );
    } );
    console.log(scene);

    // 添加轨道控制
    const controls = new OrbitControls( camera, renderer.domElement );
    // 设置阻尼
    controls.enableDamping = true
    controls.dampingFactor = 0.1;
    function animate() {
      controls.update();
      requestAnimationFrame( animate );
      // scene.rotation.x += 0.01;
      scene.rotation.y += 0.005;
      renderer.render( scene, camera );
    }
    animate();
  }
};
export default initMethod;